package struts.form;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

public class Struts_xml {
	
	public static Map<String, XmlBean> ddd(String spath) throws Exception{
		Map<String, XmlBean> xmlMap = new HashMap<>();
		
		SAXBuilder builder = new SAXBuilder();
		Document document = builder.build(new File(spath));
		Element root = document.getRootElement();
		Element actionform = root.getChild("formbeans");
		List<Element> form = actionform.getChildren();
		Element actionroot = root.getChild("action-mapping");
		List<Element> action = actionroot.getChildren();
		for(Element e:action) {
			XmlBean bean = new XmlBean();
			String path = e.getAttributeValue("path");
			bean.setPath(path);
			bean.setName(e.getAttributeValue("name"));
			bean.setType(e.getAttributeValue("type"));
			for(Element ele : form) {
				if(e.getAttributeValue("name").equals(ele.getAttributeValue("name"))){
					bean.setForm(ele.getAttributeValue("class"));
					break;
				}
			}
			List<Element> forward = e.getChildren();
			Map<String, String> map = new HashMap<String,String>();
			for(Element x:forward) {
				String fname = x.getAttributeValue("name");
				String fvalue = x.getAttributeValue("value");
				map.put(fname, fvalue);
			}
			bean.setForward(map);
			xmlMap.put(path, bean);
		}
		return xmlMap;
	}
	
	public static void main(String args[]) {
		try {
			Struts_xml.ddd("");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
